<template>
  <el-cascader
    v-model="organizeId"
    :options="organizeTreeList"
    :props="{ expandTrigger: 'hover', label: 'name', value: 'id', checkStrictly: true }"
  />
</template>

<script>
import { treeFindPath, findNodeDfs } from '@/utils/jcore/tree'
export default {
  name: 'IdentitySelect',
  props: {
    sysOrganizeId: {
      type: String,
      default: ''
    },
    sysDepartmentId: {
      type: String,
      default: ''
    },
    organizeTreeList: {
      type: Array,
      default: () => []
    }
  },
  computed: {
    organizeId: {
      get() {
        let value = treeFindPath(this.organizeTreeList, (node) => {
          return node.id == this.sysDepartmentId
        })
        return value
      },
      set(v) {
        let node = findNodeDfs(this.organizeTreeList, (node) => {
          return node.id == v.slice(-1)[0]
        })
        let sysOrganizeId = ''
        let sysDepartmentId = node.id
        if (node.pid == '/') {
          sysOrganizeId = sysDepartmentId
        } else {
          let pNode = findNodeDfs(this.organizeTreeList, (n) => {
            return n.mid == node.pid
          })
          sysOrganizeId = pNode.id
        }
        this.$emit('update:sysOrganizeId', sysOrganizeId)
        this.$emit('update:sysDepartmentId', sysDepartmentId)
      }
    }
  }
}
</script>
